/**********************************************************************************
// DemoBackground
// 
// Creation:	03 Mai 2011
// Updated:		03 Mai 2011
// Compiler:	Visual Studio 2011
//
// Notes:		Test the background class with background scrolling
//
**********************************************************************************/

#define WIN32_LEAN_AND_MEAN  // just say no to MFC

#include "Resources.h"
#include "Anabele.h"	

/**********************************************************************************/
/*                          DemoBackground Class                                   */
/**********************************************************************************/

class DemoBackground: public D3DGame
{
private:
	Input input;
	Background * backg;
	RECT viewport;

	int maxWidth;
	int maxHeight;
	int vel;

	Sprite * demobox;
	Sprite * demobar;
	char text[256];
	
public:
	DemoBackground();

	void GameInit();
	void GameLogic();
	void GameDraw();
	void GameShutdown();
	void UnloadResources();
	void LoadResources();
};

/**********************************************************************************/

DemoBackground::DemoBackground()
{
	backg = nullptr;
	demobox = nullptr;
	demobar = nullptr;

	viewport.left = 0;
	viewport.top = 0;
	viewport.right = 0;
	viewport.bottom = 0;
}


/**********************************************************************************/

void DemoBackground::GameInit()
{	
	input.Initialize();
	input.InitializeKeyboard();
	
	LoadResources();

	maxWidth = backg->GetWidth();
	maxHeight = backg->GetHeight();

	int difx = (maxWidth - GetWindowWidth())/2;
	int dify = (maxHeight - GetWindowHeight())/2;

	viewport.left = 0 + difx;
	viewport.right = viewport.left + GetWindowWidth();
	viewport.top = 0 + dify;
	viewport.bottom = viewport.top + GetWindowHeight();

	vel = 5;
}

/**********************************************************************************/

void DemoBackground::LoadResources()
{
	backg = new Background("Resources/SpaceArt.jpg");

	demobox = new Sprite("Resources/demoBox.png");
	demobox->MoveTo(155, 100);
	
	demobar = new Sprite("Resources/demoBar.png");
	demobar->MoveTo(GetWindowWidth()/2.0f, GetWindowHeight()-30.0f);
}

/**********************************************************************************/

void DemoBackground::UnloadResources()
{
	delete backg;
	if (demobar) delete demobar;
	if (demobox) delete demobox;
}

/**********************************************************************************/

void DemoBackground::GameShutdown()
{
	UnloadResources();
}


/**********************************************************************************/

void DemoBackground::GameLogic()
{
	EndsOnEscape();

	input.GetKeyboardState();

	if (input.KeyPressed(DIK_LEFT))
	{
		viewport.left -= vel;
		viewport.right -= vel;

		if (viewport.left < 0)
		{
			viewport.left  = 0;
			viewport.right = GetWindowWidth();
		}
	}

	if (input.KeyPressed(DIK_RIGHT))
	{
		viewport.left += vel;
		viewport.right += vel;

		if (viewport.right > (maxWidth))
		{
			viewport.left  = maxWidth - GetWindowWidth();
			viewport.right = maxWidth;
		}
	}

	if (input.KeyPressed(DIK_UP))
	{
		viewport.top -= vel;
		viewport.bottom -= vel;

		if (viewport.top < 0)
		{
			viewport.top  = 0;
			viewport.bottom = GetWindowHeight();
		}
	}

	if (input.KeyPressed(DIK_DOWN))
	{
		viewport.top += vel;
		viewport.bottom += vel;

		if (viewport.bottom > maxHeight)
		{
			viewport.top = maxHeight - GetWindowHeight();
			viewport.bottom = maxHeight;
		}
	}
} 

/**********************************************************************************/

void DemoBackground::GameDraw()
{
	backg->Draw(viewport);
	demobox->Draw();
	demobar->Draw();

	sprintf_s(text, "Background Demo");
	sysFont->Draw(55, 40, text);

	sprintf_s(text, "Loading image to surface");
	sysFont->Draw(55, 80, text);

	sprintf_s(text, "Scrolling the background");
	sysFont->Draw(55, 100, text);

	sprintf_s(text, "Background size: %d x %d", maxWidth, maxHeight);
	sysFont->Draw(55, 120, text);

	sprintf_s(text, "Viewport: (%d,%d) to (%d,%d)", viewport.left, viewport.top, viewport.right, viewport.bottom);
	sysFont->Draw(55, 140, text);	

	sprintf_s(text, "Use <Arrows> to Scroll the Viewport");
	sysFont->Draw(0, GetWindowHeight()-38, text, Color(255,255,255), GetWindowWidth(), 30, DT_CENTER);
} 


/**********************************************************************************/
/*                                  WinMain                                       */
/**********************************************************************************/

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	DemoBackground backdemo;

	backdemo.SetWindowMode(WINDOWED);
	backdemo.SetWindowSize(1024,768);
	backdemo.SetWindowBgColor(10,25,55);
	backdemo.SetWindowTitle(string("Background Demo"));
	backdemo.SetWindowIcon(LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)));
	backdemo.ShowWindowCursor(false);
	backdemo.Initialize(hInstance);
	backdemo.Start();
	
	return 0;

}

/**********************************************************************************/